/*
 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/*
 * @(#)StmtClient3.java	1.8 02/01/12
 */

package jdbcts.statement;

import java.io.*;
import java.util.*;
import java.math.*;

import java.sql.*;
import javax.sql.*;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.sun.cts.util.*;
import com.sun.cts.porting.*;
import com.sun.cts.harness.*;

import java.rmi.RemoteException;

import javasoft.sqe.javatest.Status;
import jdbcts.SuperTest;
import jdbcts.common.DataSourceConnection;
import jdbcts.common.DriverManagerConnection;

import com.sun.cts.tests.jdbc.ee.common.*;

// Merant DataSource class
//import com.merant.sequelink.jdbcx.datasource.*;

/**
 * The StmtClient3 class tests methods of Statement interface using Sun's J2EE
 * Reference Implementation.
 * 
 * @author
 * @version 1.7, 06/16/99
 */

public class StmtClient3 extends SuperTest implements Serializable
{
    private static final String testName = "jdbc.ee.stmt.stmt3";

    // Naming specific member variables
    private CTSNamingContextInterface jc = null;

    // Harness requirements

    private transient static Connection conn = null;

    private static Statement stmt = null;

    private DataSource ds1 = null;

    private static String drManager = null;

    private Properties sqlp = null;

    /* Run test in standalone mode */
    // public static void main(String[] args)
    // {
    // StmtClient3 theTests = new StmtClient3();
    // Status s=theTests.run(args, System.out, System.err);
    // s.exit();
    // }
    /* Test setup: */
    /*
     * @class.setup_props: Driver, the Driver name; db1, the database name with
     * url; user1, the database user name; password1, the database password;
     * db2, the database name with url; user2, the database user name;
     * password2, the database password; DriverManager, flag for DriverManager;
     * ptable, the primary table; ftable, the foreign table; cofSize, the
     * initial size of the ptable; cofTypeSize, the initial size of the ftable;
     * binarySize, size of binary data type; varbinarySize, size of varbinary
     * data type; longvarbinarySize, size of longvarbinary data type;
     * 
     * @class.testArgs: -ap ctssql.stmt
     */
    @BeforeClass
    // public void setup(String[] args, Properties p) throws Exception
    public static void setup() throws Exception
    {
	try
	{
	    try
	    {
		// drManager = p.getProperty("DriverManager","");
		drManager = isDRManager;
		if (drManager.length() == 0)
		    throw new Exception("Invalid DriverManager Name");
		// sqlp=p;

		if (drManager.equals("yes"))
		{
		    logMsg("Using DriverManager");
		    DriverManagerConnection dmCon = new DriverManagerConnection();
		    // conn = dmCon.getConnection(p);
		    conn = dmCon.getConnection(null);
		}
		else
		{
		    logMsg("Using DataSource");
		    DataSourceConnection dsCon = new DataSourceConnection();
		    // conn = dsCon.getConnection(p);
		    conn = dsCon.getConnection(null);
		}
		stmt = conn.createStatement();
	    }
	    catch (SQLException ex)
	    {
		logErr("SQL Exception : " + ex.getMessage());
	    }
	}
	catch (Exception e)
	{
	    logErr("Setup Failed!");
	    TestUtil.printStackTrace(e);
	}
    }

    /*
     * @testName: testSetFetchSize05
     * 
     * @assertion: The Statement object provides methods for executing SQL
     * statements and retrieving results.(See section 40.1 of JDBC 2.0 API
     * Reference & Tutorial second edition).
     * 
     * The driver must provide full support for Statement methods. The driver
     * must also support all the methods for executing SQL Statements in a
     * single batch (Batch Updates). (See section 6.2.2.3 of Java2 Platform
     * Enterprise Edition(J2EE) Specification v1.2)
     * 
     * The setFetchSize(int size) method sets the number of rows to fetch from
     * the database specified by the value size. The method does not return any
     * value and throws SQLException if a database access error occurs or the
     * condition 0 <= size <= this.getMaxRows is not satisfied. (See JDK 1.2.2
     * API Documentation)
     * 
     * @test_Strategy: Get a Statement object and call the setFetchSize(int
     * rows) method with the negative value and it should throw SQLException
     */
    @Test
    public void testSetFetchSize05() throws Exception
    {
	int maxFetchSizeVal = 0;
	String sMaxFetchSizeVal = null;
	boolean sqlExceptFlag = false;
	try
	{

	    // sMaxFetchSizeVal = sqlp.getProperty("Max_Set_Val","");
	    // maxFetchSizeVal=Integer.parseInt(sMaxFetchSizeVal);
	    maxFetchSizeVal = maxSetValue;
	    maxFetchSizeVal = maxFetchSizeVal * (-1);
	    logMsg("Maximum Value to be set as Fetch Size " + maxFetchSizeVal);

	    logMsg("Calling setFetchSize method");
	    try
	    {
		stmt.setFetchSize(maxFetchSizeVal);
	    }
	    catch (SQLException sqle)
	    {
		sqlExceptFlag = true;
	    }
	    if (sqlExceptFlag)
	    {
		logMsg("setFetchSize method does not set the invalid value ");
	    }
	    else
	    {
		logErr("setFetchSize method sets the Invalid value ");
		throw new Exception("Call to setFetchSize is Failed");
	    }
	}
	catch (Exception e)
	{
	    logErr("Unexpected exception " + e.getMessage());
	    throw new Exception("Call to setFetchSize is Failed!", e);
	}
    }

    /*
     * @testName: testSetMaxFieldSize01
     * 
     * @assertion: The Statement object provides methods for executing SQL
     * statements and retrieving results.(See section 40.1 of JDBC 2.0 API
     * Reference & Tutorial second edition).
     * 
     * The driver must provide full support for Statement methods. The driver
     * must also support all the methods for executing SQL Statements in a
     * single batch (Batch Updates). (See section 6.2.2.3 of Java2 Platform
     * Enterprise Edition(J2EE) Specification v1.2)
     * 
     * The setMaxFieldSize(int maxsize) method sets the maximum size for a
     * column in a result set specified by the value maxsize (in bytes). For
     * maximum portability, the maximum field size should be set to a value
     * greater than 256. If the value maxsize is 0 then it means that there is
     * no limit to the size of a column. The setMaxFieldSize(int maxsize) does
     * not return any value. (See JDK 1.2.2 API Documentation)
     * 
     * @test_Strategy: Get a Statement object and call the setMaxFieldSize(int
     * max) method and call getMaxFieldSize() method and it should return an
     * integer value that is been set
     */
    @Test
    public void testSetMaxFieldSize01() throws Exception
    {
	int maxFieldSizeVal = 0;
	String sMaxFieldSizeVal = null;
	int retVal = 0;
	try
	{
	    // sMaxFieldSizeVal=sqlp.getProperty("Max_Set_Val","");
	    // maxFieldSizeVal=Integer.parseInt(sMaxFieldSizeVal);
	    maxFieldSizeVal = maxSetValue;

	    // for Maximum Portability the value is multiplied with 256
	    maxFieldSizeVal = maxFieldSizeVal * 256;
	    logMsg("Maximum Field Size Value to be set " + maxFieldSizeVal);

	    logMsg("Calling maxFieldSize method ");
	    stmt.setMaxFieldSize(maxFieldSizeVal);

	    // invoke on the getMaxFieldSize
	    retVal = stmt.getMaxFieldSize();

	    if (maxFieldSizeVal == retVal)
	    {
		logMsg("setMaxFieldSize method sets the value for Maximum Field Size");
	    }
	    else
	    {
		logErr("setMaxFieldSize method does not set the value for Maximum Field Size");
		throw new Exception("Call to setMaxFieldSize is Failed");
	    }
	}
	catch (SQLException sqle)
	{
	    logErr("SQL Exception " + sqle.getMessage());
	    throw new Exception("Call to setMaxFieldSize is Failed!", sqle);
	}
	catch (Exception e)
	{
	    logErr("Unexpected exception " + e.getMessage());
	    throw new Exception("Call to setMaxFieldSize is Failed!", e);
	}
    }

    /*
     * @testName: testSetMaxFieldSize02
     * 
     * @assertion: The Statement object provides methods for executing SQL
     * statements and retrieving results.(See section 40.1 of JDBC 2.0 API
     * Reference & Tutorial second edition).
     * 
     * The driver must provide full support for Statement methods. The driver
     * must also support all the methods for executing SQL Statements in a
     * single batch (Batch Updates). (See section 6.2.2.3 of Java2 Platform
     * Enterprise Edition(J2EE) Specification v1.2)
     * 
     * The setMaxFieldSize(int maxsize) method sets the maximum size for a
     * column in a result set specified by the value maxsize (in bytes). For
     * maximum portability, the maximum field size should be set to a value
     * greater than 256. If the value maxsize is 0 then it means that there is
     * no limit to the size of a column. The setMaxFieldSize(int maxsize) does
     * not return any value. (See JDK 1.2.2 API Documentation)
     * 
     * @test_Strategy: Get a Statement object and call the setMaxFieldSize(int
     * max) method with an invalid value (negative value) and It should throw a
     * SQLException
     */
    @Test
    public void testSetMaxFieldSize02() throws Exception
    {
	int maxFieldSizeVal = 0;
	String sMaxFieldSizeVal = null;
	boolean sqlExceptFlag = false;

	try
	{
	    // sMaxFieldSizeVal=sqlp.getProperty("Max_Set_Val","");
	    // maxFieldSizeVal=Integer.parseInt(sMaxFieldSizeVal);
	    maxFieldSizeVal = maxSetValue;
	    maxFieldSizeVal = maxFieldSizeVal * (-1);
	    logMsg("Rows Value to be set " + maxFieldSizeVal);

	    logMsg("Calling the setMaxFieldSize method");
	    try
	    {
		stmt.setMaxFieldSize(maxFieldSizeVal);
	    }
	    catch (SQLException sqe)
	    {
		sqlExceptFlag = true;
	    }

	    if (sqlExceptFlag)
	    {
		logMsg("setMaxFieldSize method does not set the Invalid value ");
	    }
	    else
	    {
		logErr("setMaxFieldSize method sets the Invalid value");
		throw new Exception("Call to setMaxFieldSize is Failed");
	    }
	}
	catch (Exception e)
	{
	    logErr("Unexpected exception " + e.getMessage());
	    throw new Exception("Call to setMaxFieldSize is Failed!", e);
	}
    }

    /*
     * @testName: testSetMaxRows01
     * 
     * @assertion: The Statement object provides methods for executing SQL
     * statements and retrieving results.(See section 40.1 of JDBC 2.0 API
     * Reference & Tutorial second edition).
     * 
     * The driver must provide full support for Statement methods. The driver
     * must also support all the methods for executing SQL Statements in a
     * single batch (Batch Updates). (See section 6.2.2.3 of Java2 Platform
     * Enterprise Edition(J2EE) Specification v1.2)
     * 
     * The setMaxRows(int maxsize) method sets the maximum number of rows that
     * any ResultSet object can contain is specified by the value maxsize. If
     * the value maxsize is 0 then it means that there is no limit. The
     * setMaxRows(int maxsize) does not return any value.(See JDK 1.2.2 API
     * Documentation)
     * 
     * @test_Strategy: Get a Statement object and call the setMaxRows(int rows)
     * method and call getMaxRows() method and it should return a integer value
     * that is been set
     */
    @Test
    public void testSetMaxRows01() throws Exception
    {
	int maxRowsVal = 0;
	String sMaxRowsVal = null;
	int retVal = 0;
	try
	{
	    // sMaxRowsVal=sqlp.getProperty("Max_Set_Val","");
	    // maxRowsVal=Integer.parseInt(sMaxRowsVal);
	    maxRowsVal = maxSetValue;
	    logMsg("Maximum Rows Value to be set " + maxRowsVal);

	    logMsg("Calling maxRowsVal method");
	    stmt.setMaxRows(maxRowsVal);

	    // invoke getMaxRows
	    retVal = stmt.getMaxRows();

	    if (maxRowsVal == retVal)
	    {
		logMsg("setMaxRows method sets the value for Maximum Rows");
	    }
	    else
	    {
		logErr("setMaxRows method does not set the value for Maximum Rows");
		throw new Exception("Call to setMaxRows is Failed");
	    }
	}
	catch (SQLException sqle)
	{
	    logErr("SQL Exception " + sqle.getMessage());
	    throw new Exception("Call to setMaxRows is Failed!", sqle);
	}
	catch (Exception e)
	{
	    logErr("Unexpected exception " + e.getMessage());
	    throw new Exception("Call to setMaxRows is Failed!", e);
	}
    }

    /*
     * @testName: testSetMaxRows02
     * 
     * @assertion: The Statement object provides methods for executing SQL
     * statements and retrieving results.(See section 40.1 of JDBC 2.0 API
     * Reference & Tutorial second edition).
     * 
     * The driver must provide full support for Statement methods. The driver
     * must also support all the methods for executing SQL Statements in a
     * single batch (Batch Updates). (See section 6.2.2.3 of Java2 Platform
     * Enterprise Edition(J2EE) Specification v1.2)
     * 
     * The setMaxRows(int maxsize) method sets the maximum number of rows that
     * any ResultSet object can contain is specified by the value maxsize. If
     * the value maxsize is 0 then it means that there is no limit. The
     * setMaxRows(int maxsize) does not return any value.(See JDK 1.2.2 API
     * Documentation)
     * 
     * @test_Strategy: Get a Statement object and call the setMaxRows(int rows)
     * method with an invalid value (negative value) and It should throw an
     * SQLException
     */
    @Test
    public void testSetMaxRows02() throws Exception
    {
	int maxRowsVal = 0;
	String sMaxRowsVal = null;
	boolean sqlExceptFlag = false;

	try
	{
	    // sMaxRowsVal=sqlp.getProperty("Max_Set_Val","");
	    // maxRowsVal=Integer.parseInt(sMaxRowsVal);
	    maxRowsVal = maxSetValue;
	    maxRowsVal = maxRowsVal * (-1);
	    logMsg("Rows Value to be set " + maxRowsVal);

	    logMsg("Calling setMaxRows method");
	    try
	    {
		stmt.setMaxRows(maxRowsVal);
	    }
	    catch (SQLException sqe)
	    {
		sqlExceptFlag = true;
	    }

	    if (sqlExceptFlag)
	    {
		logMsg("setMaxRows method does not set the Invalid value ");
	    }
	    else
	    {
		logErr("setMaxRows method sets the Invalid value");
		throw new Exception("Call to setMaxRows is Failed");
	    }
	}
	catch (Exception e)
	{
	    logErr("Unexpected exception " + e.getMessage());
	    throw new Exception("Call to setMaxRows is Failed!", e);
	}
    }

    /*
     * @testName: testSetQueryTimeout02
     * 
     * @assertion: The Statement object provides methods for executing SQL
     * statements and retrieving results.(See section 40.1 of JDBC 2.0 API
     * Reference & Tutorial second edition).
     * 
     * The driver must provide full support for Statement methods. The driver
     * must also support all the methods for executing SQL Statements in a
     * single batch (Batch Updates). (See section 6.2.2.3 of Java2 Platform
     * Enterprise Edition(J2EE) Specification v1.2)
     * 
     * The setQueryTimeout(int secval) method sets the time limit for the number
     * of secval seconds a driver will wait for a statement object to be
     * executed. If the value secval is 0 then it means that there is no limit.
     * The setQueryTimeout method does not return any value. (See JDK 1.2.2 API
     * Documentation)
     * 
     * @test_Strategy: Get a Statement object and call the setQueryTimeout(int
     * secval) method with an invalid value (negative value)and It should throw
     * an SQLException
     */
    @Test
    public void testSetQueryTimeout02() throws Exception
    {
	int maxQueryTimeVal = 0;
	String sMaxQueryTimeVal = null;
	boolean sqlExceptFlag = false;

	try
	{
	    // sMaxQueryTimeVal=sqlp.getProperty("Max_Set_Val","");
	    // maxQueryTimeVal=Integer.parseInt(sMaxQueryTimeVal);
	    maxQueryTimeVal = maxSetValue;
	    maxQueryTimeVal = maxQueryTimeVal * (-1);
	    logMsg("Seconds Value to be set as QueryTimeout " + maxQueryTimeVal);

	    logMsg("Calling maxQueryTimeout method");
	    try
	    {
		stmt.setQueryTimeout(maxQueryTimeVal);
	    }
	    catch (SQLException sqe)
	    {
		sqlExceptFlag = true;
	    }

	    if (sqlExceptFlag)
	    {
		logMsg("setQueryTimeout method does not set the Invalid value ");
	    }
	    else
	    {
		logErr("setQueryTimeout method sets the Invalid value");
		throw new Exception("Call to setQueryTimeout is Failed");
	    }
	}
	catch (Exception e)
	{
	    logErr("Unexpected exception " + e.getMessage());
	    throw new Exception("Call to setQueryTimeout is Failed!", e);
	}
    }

    /* cleanup */
    @AfterClass
    public static void cleanup() throws Exception
    {
	try
	{
	    // Close the Statement object
	    stmt.close();
	    conn.close();
	    logMsg("Cleanup ok;");
	}
	catch (Exception e)
	{
	    logErr("An error occurred while closing the database connection");
	}
    }

}
